package day02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * list.sort(Comparator<? super E> c)传入的比较器
 */
public class StudentTest {
    public static void main(String[] args) {

    List<Student> list = new ArrayList<>();
    Student stu1 = new Student(1003,"tom",23);
    Student stu2 = new Student(1002,"jerry",22);
    Student stu3 = new Student(1006,"tony",21);
    Student stu4 = new Student(1004,"tod",28);
    Student stu5 = new Student(1001,"andy",26);
        Collections.addAll(list,stu1,stu2,stu3,stu4,stu5);
//        list.sort(Student::compareTo);//按id自然升序排列，list的sort()方法，传入的参数是比较器
//        Collections.sort(list, (o1, o2) -> o2.getAge()-o1.getAge());
//        Collections.sort(list, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o2.getAge()-o1.getAge();
//            }
//        });
//        Collections.sort(list,Student::compareTo);
//        根据姓名倒序排列
//        Collections.sort(list, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o2.getName().compareTo(o1.getName());
//            }
//        });
        Collections.sort(list,(o1,o2)-> o2.getName().compareTo(o1.getName()));
        list.forEach(i-> System.out.println(i));
//        按年龄降序排列
        list.sort((o1, o2) -> o2.getAge()-o1.getAge());
        list.forEach(i-> System.out.println(i));

    }
}

